home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3048 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.2 KB  |  64 lines

  1. Path: news.uh.edu!usenet
  2. From: Sensarn <txs53132@bayou.uh.edu>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Basic header file broblem in BC 4.5
  5. Date: 21 Jan 1996 22:26:17 GMT
  6. Organization: AEtna Insurance Agency
  7. Message-ID: <4duei9$pgb@masala.cc.uh.edu>
  8. References: <4du8q0$k1h@yuggoth.ucsb.edu>
  9. NNTP-Posting-Host: sip-14262.public-dialups.uh.edu
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1 (Windows; U; 16bit)
  14.  
  15. The CPP files work seperately:
  16.  
  17. If your project window looked like this:
  18.  
  19. main.cpp
  20. function.cpp
  21.  
  22. You simply have two CPP files that are to be compiled together.  Each 
  23. file needs prototypes.  The main.cpp file is the one with the main() 
  24. routine -- prototypes for main.cpp are located in main.cpp.  To use the 
  25. functions in function.cpp, main.cpp must also contain the prototypes for 
  26. function.cpp.  Therefore, you create a header file for function.cpp -- 
  27. we'll call it function.h.  In the header file, you have prototypes and 
  28. structures:
  29.  
  30. void xxxx(void);  //Whatever
  31. ..
  32.  
  33. To use this header file, you add:
  34.  
  35. #include "function.h"
  36.  
  37. to main.cpp.  This is EXACTLY the same thing as copying ALL of the data 
  38. from function.h to main.cpp (the first line of function.h goes on the 
  39. line where #include "function.h" is located).  This means that if the 
  40. first line in function.h is:
  41.  
  42. void nothing(void);
  43.  
  44. the main.cpp file would look somewhat like this:
  45.  
  46. #include "function.h" //This part may be deleted -- I don't know exactly
  47.                       //how the compiler works.
  48. void nothing(void);
  49.  
  50. Good so far.  Now main.cpp has all the prototypes for function.cpp.  
  51. Unfortunately, in our example, function.cpp does not contain any 
  52. prototypes.  We must also include function.h to this module.  Now we 
  53. can't define global variables in function.h; we would receive an error 
  54. because we defined the variables twice (once in function.cpp and once in 
  55. main.cpp).  To do this, we create main.h.  The global variables are 
  56. defined here.  Main.h is #included in main.cpp but NOT in function.cpp.  
  57. Good for us; our globals are only defined once.
  58.  
  59. This may not be the best way to organize your work, but I haven't had any 
  60. problems with it.
  61.  
  62. Steven Sensarn - txs53132@bayou.uh.edu
  63.  
  64.